﻿Friend Class DBInfo
    Public Enum DBInfoType
        MSSQL
        MySql
        SQLite
        PostGreSQL
        Oracle
    End Enum

    Public Shared Connections As New Generic.Dictionary(Of String, iDBInfo)(StringComparer.OrdinalIgnoreCase)

    Public Shared Function NoConnection(connectionName As String) As String
        If connectionName = "Default" Then
            Return "There is no active Sql Connection."
        Else
            Return String.Format("There is no active Sql Connection ({0}).", connectionName)
        End If
    End Function

    Public Class Map
        Public Ordinal As Integer
        Public Name As String
        Public AllowNull As Boolean
        Public DataType As Type
        Public MappedName As String
        Public Sub New(position As Integer, name As String, type As Type, null As Boolean)
            Me.Ordinal = position
            Me.Name = name
            Me.DataType = type
            Me.AllowNull = null
        End Sub
    End Class

    Friend Shared Sub ProcessHashTable(ByRef ht As Hashtable)
        If ht IsNot Nothing Then
            For Each key As String In ht.Keys.Cast(Of String).ToArray
                If ht(key) IsNot Nothing AndAlso ht(key).GetType = GetType(PSObject) Then ht(key) = DirectCast(ht(key), PSObject).BaseObject
            Next
        End If
    End Sub

    Friend Shared Function ConvertToPSObject(dr As IDataRecord, map As Generic.List(Of Map)) As PSObject
        Dim ps As New PSObject
        For Each m As Map In map
            With ps.Members
                If m.AllowNull AndAlso dr.IsDBNull(m.Ordinal) Then
                    .Add(New PSNoteProperty(m.Name, Nothing), True)
                Else
                    Select Case m.DataType
                        Case GetType(System.Boolean)
                            .Add(New PSNoteProperty(m.Name, dr.GetBoolean(m.Ordinal)), True)
                        Case GetType(System.Byte)
                            .Add(New PSNoteProperty(m.Name, dr.GetByte(m.Ordinal)), True)
                        Case GetType(System.Char)
                            .Add(New PSNoteProperty(m.Name, dr.GetChar(m.Ordinal)), True)
                        Case GetType(System.DateTime)
                            .Add(New PSNoteProperty(m.Name, dr.GetDateTime(m.Ordinal)), True)
                        Case GetType(System.Decimal)
                            .Add(New PSNoteProperty(m.Name, dr.GetDecimal(m.Ordinal)), True)
                        Case GetType(System.Double)
                            .Add(New PSNoteProperty(m.Name, dr.GetDouble(m.Ordinal)), True)
                        Case GetType(System.Single)
                            .Add(New PSNoteProperty(m.Name, dr.GetFloat(m.Ordinal)), True)
                        Case GetType(System.Guid)
                            .Add(New PSNoteProperty(m.Name, dr.GetGuid(m.Ordinal)), True)
                        Case GetType(System.Int16)
                            .Add(New PSNoteProperty(m.Name, dr.GetInt16(m.Ordinal)), True)
                        Case GetType(System.Int32)
                            .Add(New PSNoteProperty(m.Name, dr.GetInt32(m.Ordinal)), True)
                        Case GetType(System.Int64)
                            .Add(New PSNoteProperty(m.Name, dr.GetInt64(m.Ordinal)), True)
                        Case GetType(System.String)
                            .Add(New PSNoteProperty(m.Name, dr.GetString(m.Ordinal)), True)
                        Case Else
                            .Add(New PSNoteProperty(m.Name, dr.GetValue(m.Ordinal)), True)
                    End Select
                End If
            End With
        Next
        Return ps
    End Function

    Friend Shared Function TestServer(host As String) As Boolean
        host = Net.Dns.GetHostEntry(host).HostName
        Using np As New Net.NetworkInformation.Ping
            Return np.Send(host, 5000).Status = Net.NetworkInformation.IPStatus.Success
        End Using
    End Function

    Friend Shared Function TestServerPort(host As String, port As Integer) As Boolean
        host = Net.Dns.GetHostEntry(host).HostName
        Using ns As New Net.Sockets.TcpClient
            Dim ah As IAsyncResult = ns.BeginConnect(host, port, Nothing, Nothing)
            Return ah.AsyncWaitHandle.WaitOne(500, False)
        End Using
    End Function
End Class
